package com.milks.dao.impl;


import org.springframework.stereotype.Repository;





import com.milks.dao.IUserDao;
import com.milks.entity.Users;

@Repository("userHibernateDao")
public class UserHibernateDao extends BaseDaoKHImpl<Users> implements IUserDao {

	@Override
	public long getGroupUserCount(int gid) {
		String hql = "select count(*) from User where group.id=?";
		long count = (Long)this.getSession().createQuery(hql)
					.setParameter(0, gid).uniqueResult();
		return count;
	}

	@Override
	public void deleteByGroup(int gid) {
		String hql = "delete User u where u.group.id=?";
		this.getSession().createQuery(hql).setParameter(0, gid).executeUpdate();
	}
	
	public Users loadByUsername(String username) {
		return (Users)this.getSession().createQuery("from Users where name=?")
					.setParameter(0, username).uniqueResult();
	}
	
	
	public Users login(String username, String password) {
		return (Users)this.getSession().createQuery("from Users u where u.name=? and u.password=?")
					.setParameter(0, username).setParameter(1, password).uniqueResult();
	}

	@Override
	public Users loadByEmail(String email) {
		return (Users)this.getSession().createQuery("from Users where email=?")
				.setParameter(0, email).uniqueResult();
	}
	
}
